<template>
  <div class="login-container1">
    <div class="loginbox">
      <el-card color=" rgba(255, 255, 255, 0)">
        <template #header>
          登录
        </template>
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
          <el-form-item label="用户名:" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密&nbsp;&nbsp;&nbsp;&nbsp;码:" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <!-- 身份 管理员 1 作者 2 用户 3 -->
          <el-form-item label="身&nbsp;&nbsp;&nbsp;&nbsp;份:" prop="identity">
            <el-select v-model="loginForm.identity" placeholder="请选择">
              <el-option label="管理员" value="1"></el-option>
              <el-option label="作  者" value="2"></el-option>
              <el-option label="用  户" value="3"></el-option>
            </el-select>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button type="primary" @click="login" class="loginbt">登录</el-button>
            <!-- 注册 -->
            <el-button type="primary" @click="register">注册</el-button>
            <!-- 重置 -->
            <el-button type="primary" @click="reset">重置</el-button>
            <!-- 找回 -->
            <el-button type="primary" @click="find">找回</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>


  </div>
</template>

<script setup>
import router from '@/router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'

const loginForm = ref({
  username: '',
  password: '',
  identity: ''
})

const loginRules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  identity: [{ required: true, message: '请选择身份', trigger: 'change' }]
})

const loginFormRef = ref()

const login = () => {
  // 判断身份
  // 管理员页面
  if (loginForm.value.identity === '1') {
    axios({
      method: 'POST',
      url: '/api/login',
      params: {
        username: loginForm.value.username,
        password: loginForm.value.password,
        identity: loginForm.value.identity
      }
    }).then((res) => {
      if (res.data.code == 200) {
        ElMessage.success(res.data.msg)
        router.push({
          path: '/admin',
          query: {
            username: loginForm.value.username,
            password: loginForm.value.password,
            identity: loginForm.value.identity
          }
        })

      } else {
        if (res.data.code == 400) {
          ElMessage.error(res.data.msg)
        } else {
          ElMessage.error('登录失败')
        }
      }
    })

  }  
  // 作者页面 
  else  if (loginForm.value.identity == '2') {
     axios({
      method: 'POST',
      url: '/api/login',
      params: {
        username: loginForm.value.username,
        password: loginForm.value.password,
        identity: loginForm.value.identity
      }
    }).then((res) => {
      if (res.data.code == 200) {
        ElMessage.success(res.data.msg)
        router.push({
          path: '/writer',
          query: {
            username: loginForm.value.username,
            password: loginForm.value.password,
            identity: loginForm.value.identity
          }
        })

      } else {
        if (res.data.code == 400) {
          ElMessage.error(res.data.msg)
        } else {
          ElMessage.error('登录失败')
        }
      }
    })
   


  }
  // 用户页面
  else if(loginForm.value.identity == '3'){
     axios({
      method: 'POST',
      url: '/api/login',
      params: {
        username: loginForm.value.username,
        password: loginForm.value.password,
        identity: loginForm.value.identity
      }
    }).then((res) => {
      if (res.data.code == 200) {
        ElMessage.success(res.data.msg)
        router.push({
          path: '/user',
          query: {
            username: loginForm.value.username,
            password: loginForm.value.password,
            identity: loginForm.value.identity
          }
        })

      } else {
        if (res.data.code == 400) {
          ElMessage.error(res.data.msg)
        } else {
          ElMessage.error('登录失败')
        }
      }
    })

  }
  else {
    ElMessage.error('请选择身份')
    return
  }
}

const register = () => {
 router.push('/register')
}

const reset = () => {
  loginFormRef.value.resetFields()
}

const find = () => {
  router.push('/retriever')
}
</script>
<style scoped>
.login-container1 {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("@/assets/images/loginbg.jpg") no-repeat center center fixed;
  background-size: cover;
}

.loginbox {
  z-index: 1;
  width: 400px;
  margin: 15% 40%;
  background-color: rgba(255, 255, 255, 0.7);
}

/* 使 el-card 透明 */
.el-card {
  background-color: transparent !important;
  border: none !important;
  box-shadow: none !important;
}

/* 保持 el-card 内容区域的可读性 */
.el-card__body {
  background-color: rgba(255, 255, 255, 0.7);
  padding: 20px;
  border-radius: 8px;
}

/* 输入框透明 */
.el-input__inner {
  background-color: rgba(255, 255, 255, 0.7) !important;
  border: 1px solid rgba(0, 0, 0, 0.1) !important;
}

/* 输入框聚焦时的样式 */
.el-input__inner:focus {
  border-color: #409EFF !important;
}

/* 选择框透明 */
.el-select .el-input__inner {
  background-color: rgba(255, 255, 255, 0.7) !important;
  border: 1px solid rgba(0, 0, 0, 0.1) !important;
}

/* 选择框聚焦时的样式 */
.el-select .el-input__inner:focus {
  border-color: #409EFF !important;
}

/* 按钮透明 */
.el-button {
  background-color: rgba(64, 158, 255, 0.7) !important;
  border-color: rgba(64, 158, 255, 0.7) !important;
}

/* 按钮悬停时的样式 */
.el-button:hover {
  background-color: rgba(64, 158, 255, 0.9) !important;
}

.el-card__header {
  font-size: 20px;
  text-align: center;
  background-color: rgba(255, 255, 255, 0.7);
}

.el-form-item__content button {
  margin-right: 10px;
}

.loginbt {
  margin-left: 10%;
}
</style>


<!--  旧版css
<style scoped>
.login-container1 {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("@/assets/images/loginbg.jpg") no-repeat center center fixed;
  background-size: cover;

}



.loginbox {
  z-index: 1;
  width: 400px;
  margin: 15% 40%;
}

.el-card__header {
  font-size: 20px;
  text-align: center;

}

.el-form-item__content button {
  margin-right: 10px;

}

.loginbt {
  margin-left: 10%;
}
</style> -->